package com.nhjf.common.util;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 加解密类
 * @author huangjingying
 * @company 浙江汇购科技有限公司
 * 2012-4-12
 * sql-webServer
 * com.nhjf.common.util.tools
 * @version 1.0
 *
 */
public class DESUtil2 {
	public static final String KEY_STRING = "farawayfrom";//生成密钥的字符串  
	static Key key;

	/**
	 * 将 s 进行 BASE64 编码
	 * @param s
	 * @return
	 */
	public static String getBASE64(String s) {
		if (s == null)
			return null;
		return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
	}

	/**
	 * 将 BASE64 编码的字符串 s 进行解码
	 * @param s
	 * @return
	 */
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

	/**    
	 * 根据参数生成KEY    
	 *     
	 * @param strKey    
	 */
/*	public static void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			_generator.init(new SecureRandom(strKey.getBytes()));
			key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		}
	}*/
	public static void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(strKey.getBytes());
			_generator.init(secureRandom);
			key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil2.class, e.getMessage());
		}
	}

	/**    
	 * 加密String明文输入,String密文输出    
	 *     
	 * @param strMing    
	 * @return    
	 */
	public static String getEncString(String strMing) {
		DESUtil2.getKey(KEY_STRING);// 生成密匙       
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";
		BASE64Encoder base64en = new BASE64Encoder();
		try {
			byteMing = strMing.getBytes("UTF8");
			byteMi = getEncCode(byteMing);
			strMi = base64en.encode(byteMi);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil2.class, e.getMessage());
		} finally {
			base64en = null;
			byteMing = null;
			byteMi = null;
		}
		return strMi;
	}

	/**    
	 * 解密 以String密文输入,String明文输出    
	 *     
	 * @param strMi    
	 * @return    
	 */
	public static String getDesString(String strMi) {
		DESUtil2.getKey(KEY_STRING);// 生成密匙       
		BASE64Decoder base64De = new BASE64Decoder();
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
		try {
			byteMi = base64De.decodeBuffer(strMi);
			byteMing = getDesCode(byteMi);
			strMing = new String(byteMing, "UTF8");
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil2.class, e.getMessage());
		} finally {
			base64De = null;
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	/**    
	 * 加密以byte[]明文输入,byte[]密文输出    
	 *     
	 * @param byteS    
	 * @return    
	 */
	private static byte[] getEncCode(byte[] byteS) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byteFina = cipher.doFinal(byteS);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil2.class, e.getMessage());
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	/**    
	 * 解密以byte[]密文输入,以byte[]明文输出    
	 *     
	 * @param byteD    
	 * @return    
	 */
	private static byte[] getDesCode(byte[] byteD) {
		Cipher cipher;
		byte[] byteFina = null;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(byteD);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil2.class, e.getMessage());
		} finally {
			cipher = null;
		}
		return byteFina;

	}

	public static void main(String[] args) {
		//URL加密
		String str =  "Hello World!" ;
	// Base64.isBase64("SGVsbG8gV29ybGQh");
		DESUtil2  d  = new DESUtil2();
		  
		System.out.println("解密："+d.getEncString (str));
	 
	}
}
